﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_FlatManger_frm_UserManage : System.Web.UI.Page
{
    public void EditResult(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsUserLoginDAL cls_User = new ClsUserLoginDAL();
            int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);
            int intIndex = Int32.Parse(((ImageButton)sender).CommandArgument);

            TextBox txt3 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txt3");
            TextBox txt4 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txt4");
            TextBox txt5 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txt5");
            TextBox txt6 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txt6");
            DropDownList drop0 = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drop0");
            DropDownList drop1 = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drop1");
            DropDownList drop2 = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drop2");
            DropDownList drop3 = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("drop3");
            Label chkRsSelect = (Label)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
            ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
            ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
            ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");

            if (intCommandName == 0)
            {
                txt3.Visible = txt4.Visible = txt5.Visible = txt6.Visible = false;
                drop0.Visible = drop1.Visible = drop2.Visible = drop3.Visible = true;
                ibtnRsCancel.Visible = ibtnRsServer.Visible = true;
                chkRsSelect.Visible = ibtnRsEdit.Visible = false;
            }
            else if (intCommandName == 1)
            {
                this.GetSysUserData(strUserID);
            }
            else
            {
                int intID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[3].Text.Trim());
                Boolean is_Succeed = cls_User.UpdateUsetState(intID, Int32.Parse(drop0.SelectedItem.Value), Int32.Parse(drop1.SelectedItem.Value), Int32.Parse(drop2.SelectedItem.Value), drop3.SelectedItem.Value);

                if (is_Succeed)
                {
                    this.GetSysUserData(strUserID);
                }
                else
                {
                    this.lblJs.Text = "<script>alert('修改失败，请重试！');</script>";
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindSysUserData(dst, intPageIndex);
        }
    }

    private void GetSysUserData(string UserID)
    {

        ClsUserLoginDAL cls_User = new ClsUserLoginDAL();

        DataSet dst = cls_User.GetSysUsers(UserID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetCorporationDataSet = dst;

            this.BindSysUserData(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = false;
        }
    }

    private void BindSysUserData(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                this.GetSysUserData(strUserID);
            }
            else
            {
                this.lblJs.Text = "";
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底作废将无法恢复数据。您真的要作废所选的项吗？');");
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            int intOverDate = Int32.Parse(e.Item.Cells[1].Text.Trim());
            int intUserType = Int32.Parse(e.Item.Cells[4].Text.Trim()) - 1;
            bool bolIsStop = bool.Parse(e.Item.Cells[2].Text.Trim());
            TextBox txt3 = (TextBox)e.Item.FindControl("txt3");
            TextBox txt4 = (TextBox)e.Item.FindControl("txt4");
            TextBox txt5 = (TextBox)e.Item.FindControl("txt5");
            TextBox txt6 = (TextBox)e.Item.FindControl("txt6");
            ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
            DropDownList drop0 = (DropDownList)e.Item.FindControl("drop0");
            DropDownList drop1 = (DropDownList)e.Item.FindControl("drop1");
            DropDownList drop2 = (DropDownList)e.Item.FindControl("drop2");
            DropDownList drop3 = (DropDownList)e.Item.FindControl("drop3");
            Label chkRsSelect = (Label)e.Item.FindControl("chkRsSelect");

            chkRsSelect.Text = "<input type=radio name='myradiogroup' value=" + e.Item.Cells[3].Text + ">";

            //密码有效期
            drop0.Items[intOverDate].Selected = true;
            txt3.Text = drop0.SelectedItem.Text;

            //停用状态
            if (!bolIsStop)
            {
                txt4.Text = "启用";
                drop1.Items[0].Selected = true;
            }
            else
            {
                txt4.Text = "停用";
                drop1.Items[1].Selected = true;
            }

            //操作员性质
            drop2.Items[intUserType].Selected = true;
            txt6.Text = drop2.SelectedItem.Text;
            
            //操作员岗位
            ClsStationDataDAL cls_UserStation = new ClsStationDataDAL();
            DataSet dst = cls_UserStation.GetStationDatas(1);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                drop3.Items.Clear();
                drop3.DataTextField = "dt_name";
                drop3.DataValueField = "dt_id";
                drop3.DataSource = dst;
                drop3.DataBind();

                string strID = e.Item.Cells[5].Text.Trim();
                int intItems = drop3.Items.Count - 1;

                for (int i = intItems; i >= 0; i--)
                {
                    if (drop3.Items[i].Value.Trim() == strID)
                    {
                        drop3.SelectedIndex = i;
                        txt5.Text = drop3.Items[i].Text;
                        break;
                    }
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('获取岗位信息为空，请确保岗位信息已通过确认！')</script>";
            }
        }
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindSysUserData(dst, intIndex);
                    }
                    else
                    {
                       this.lblJs.Text = "<script>alert('请输入正确的页码数！')</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            this.GetSysUserData(strUserID);
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void ibtnUpdatePwd_Click(object sender, ImageClickEventArgs e)
    {
        string strIndex = Request.Form["myradiogroup"];

        if (strIndex != null && strIndex != "")
        {
            this.lblJs.Text = "<script>ModalDialog('frm_AddUserInfo.aspx?Type=Update&UserID=" + strIndex + "', 466, 362);</script>";
        }
        else
        { 
            this.lblJs.Text = "<script>alert('请先选择操作员！');</script>";
        }
    }

    protected void ibtnView_Click(object sender, ImageClickEventArgs e)
    {
        string strIndex = Request.Form["myradiogroup"];

        if (strIndex != null && strIndex != "")
        {
            
        }
        else
        {
            this.lblJs.Text = "<script>alert('请先选择操作员！');</script>";
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteUserInfo(intCommandName);
    }

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteUserInfo(intCommandName);
    }

    private void DeleteUserInfo(int CommandName)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsUserLoginDAL cls_User = new ClsUserLoginDAL();

            string strIndex = Request.Form["myradiogroup"];

            if (strIndex != null && strIndex != "")
            {
                int intItem = Int32.Parse(strIndex);

                Boolean is_Succeed = false;

                if (CommandName == 0)
                {
                    is_Succeed = cls_User.DeleteSysUser(intItem, 0);
                }
                else
                {
                    is_Succeed = cls_User.DeleteSysUser(intItem, 1);
                }

                if (!is_Succeed)
                {
                    this.lblJs.Text = "<script>alert('作废失败，请重试！');</script>";
                }

                this.GetSysUserData(strUserID);
            }
            else
            {
                this.lblJs.Text = "<script>alert('请先选择你要作废的操作员！');</script>";
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }
}
